package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.mapper.UserMapper;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQueryObject;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private UserMapper userMapper;


    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<ServiceItem> serviceItems = serviceItemMapper.selectForList(qo);
        return new TablePageInfo<ServiceItem>(serviceItems);
    }


    @Override
    public void save(ServiceItem serviceItem) {
        //新增的时候判断他是不是套餐,如果是套餐的话就需要添加审核状态为初始化
        if (!serviceItem.getCarPackage().equals(ServiceItem.CARPACKAGE_NO)){
            serviceItem.setAuditStatus(0);
        }
        serviceItem.setCreateTime(new Date());
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        Integer saleStatus = serviceItemMapper
                .selectByPrimaryKey(serviceItem.getId())
                .getSaleStatus();
        Integer auditStatus = serviceItemMapper.selectByPrimaryKey(serviceItem.getId()).getAuditStatus();
        //已上架的产品不允许修改
        if (ServiceItem.SALESTATUS_ON.equals(saleStatus)){
            throw new   BusinessException("上架状态的商品不允许修改");
        }
        if (ServiceItem.AUDITSTATUS_AUDITING.equals(auditStatus)){
            throw new BusinessException("审核中的商品不允许修改");
        }
        //是套餐的话,修改操作会使审核通过状态变成初始化
        if (serviceItem.getCarPackage()==ServiceItem.CARPACKAGE_YES){
            serviceItemMapper.updateAuditstatus(ServiceItem.AUDITSTATUS_INIT,serviceItem.getId());
        }
        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    //上架
    public void saleOn(Long id) {
        //审核中的商品不允许上架
        //是套餐但未审核通过不允许上架
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);
        //是套餐
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){
            if (!ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())){
                throw new BusinessException("需要审核通过才能上架");
            }
        }
        serviceItemMapper.updateSaleOn(ServiceItem.SALESTATUS_ON,id);
    }

    @Override
    public void saleOff(Long id) {
        //如果是套餐下架后要初始化
        if ( ServiceItem.CARPACKAGE_YES.equals(serviceItemMapper.selectByPrimaryKey(id).getCarPackage())){
            serviceItemMapper.updateAuditstatus(ServiceItem.AUDITSTATUS_INIT,id);
        }
        serviceItemMapper.updateSaleOn(ServiceItem.SALESTATUS_OFF,id);
    }

    @Override
    public  List<User> selectDirectors() {
       return serviceItemMapper.selectDirectors();
    }

    @Override
    public  List<User> selectFinances() {
        return serviceItemMapper.selectFinances();
    }

    @Override
    public void startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {

        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);

        //判断当前服务单项不为空.是套餐,且是初始化状态

        if (serviceItem==null || ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage())||!ServiceItem.AUDITSTATUS_INIT.equals(serviceItem.getAuditStatus()) ){
            throw new BusinessException("参数异常");
        }
        //养修服务单项状态改变,审核中
        serviceItemMapper.updateAuditstatus(ServiceItem.AUDITSTATUS_AUDITING,id);


        //套餐审核列表新增一条
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        //服务单项id
        carPackageAudit.setServiceItemId(id);
        //服务单项备注
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        //服务单项折扣价
        carPackageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        //服务单项对象
        carPackageAudit.setServiceItem(serviceItem);
        //创建者
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        //创建时间
        carPackageAudit.setCreateTime(new Date());
        //当前审批人id
        carPackageAudit.setAuditorId(director);
        //审核流程对象
        carPackageAudit.setBpmnInfoId(bpmnInfoId);
        //套餐审核备注
        carPackageAudit.setInfo(info);
        //插入数据
        carPackageAuditMapper.insert(carPackageAudit);


        //启动流程,添加参数进去
        Map<String ,Object>map = new HashMap<>();
        //设置审核价格,启动流程添加数据,不支持BigDecimal类型,支持String字符串类型
        map.put("discountPrice",serviceItem.getDiscountPrice().longValue());
        //针对两个审批人,不为null就封装进去
        if (director!=null){
            map.put("director",director);
        }
        if (finance!=null){
            map.put("finance",finance);
        }

        //启动流程
        //这时候我们想到我们没有key值,但是我们可以拿到对应的bpmn(流程定义对象)对象,它里面封装了流程对象的key值
        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId);
        ProcessInstance instance = runtimeService
                //把我们设计的套餐列表id当作businessKey,封装进去;
                .startProcessInstanceByKey(bpmnInfo.getActProcessKey(), carPackageAudit.getId().toString(), map);


        //更新套餐审核流程的流程实例id
        carPackageAudit.setInstanceId(instance.getId());
        carPackageAuditMapper.update(carPackageAudit);








    }

    @Override
    public void updateByStatic(Integer auditstatusInit, Long serviceItemId) {
        serviceItemMapper.updateAuditstatus(auditstatusInit,serviceItemId);
    }
}
